package com.hotel.controller;

import com.hotel.bean.Information;
import com.hotel.bean.RespBean;
import com.hotel.exception.SteduException;
import com.hotel.service.AdminService;
import com.hotel.service.InformationService;
import com.hotel.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/admin/info")
public class InfoController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private InformationService informationService;
    @GetMapping("/roomIn")
    public RespBean selectByTop5DnameAndCount()  {
        return RespBean.ok("",adminService.selectAllRoomIn());
    }
    @GetMapping("/roomRprice")
    public RespBean selectAllRoomRprice(){
        return RespBean.ok("",adminService.selectAllRoomRprice());
    }
    @GetMapping("/todayInfo")
    public RespBean selectTodayInfo(){
        return RespBean.ok("",adminService.selectTodayInfo());
    }
    @GetMapping("/adminInfo")
        public RespBean getLoginInfo(HttpServletRequest request){
        //获取token
        String token = request.getHeader("token");
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer aid = (Integer) map.get("aid");
        return RespBean.ok("",informationService.selectByAid(aid));
    }
    @PutMapping
    public RespBean updateInfo(@RequestBody  @Validated Information information) throws SteduException {
        if(informationService.update(information)){
            return RespBean.ok("修改成功");
        }
        return RespBean.error("修改失败");
    }
}
